package com.ruoyi.patrol.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.patrol.domain.PtLine;
import com.ruoyi.patrol.domain.PtLineApp;
import com.ruoyi.patrol.domain.PtLineSite;
import com.ruoyi.patrol.domain.PtLineSiteApp;
import com.ruoyi.patrol.mapper.PtLineAppMapper;
import com.ruoyi.patrol.mapper.PtLineMapper;
import com.ruoyi.patrol.mapper.PtLineSiteMapper;
import com.ruoyi.patrol.mapper.PtLineStaffAppMapper;
import com.ruoyi.patrol.service.IPtLineAppService;
import com.ruoyi.patrol.service.IPtLineService;
import com.ruoyi.patrol.service.IPtLineSiteService;
import com.ruoyi.system.mapper.SysDictDataMapper;

import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * App端巡查线路Service业务层处理
 *
 * @author mlb
 * @date 2022-11-14
 */
@Service
public class PtLineAppServiceImpl implements IPtLineAppService {
    @Autowired
    private PtLineAppMapper ptLineAppMapper;
    @Autowired
    private PtLineStaffAppMapper ptLineStaffAppMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询巡查线路
     *
     * @param id 巡查线路ID
     * @return 巡查线路
     */
    @Override
    public PtLineApp selectPtLineById(Long id) {
        PtLineApp line = null;
        // 按照行政与保安不同，分为两个数据表操作
        if (SecurityUtils.getGuardId() != null) {
            line = ptLineAppMapper.selectPtLineById(id);
        } else if (SecurityUtils.getStaffId() != null) {
            line = ptLineStaffAppMapper.selectPtLineById(id);
        }

        return line;
    }

    /**
     * 查询巡查线路列表
     *
     * @param ptLine 巡查线路
     * @return 巡查线路
     */
    @Override
    @DataScope(userAlias2 = "s")
    public List<PtLineApp> selectPtLineList(PtLine ptLine) {
        List<PtLineApp> lines = null;
        // 从字典获取半径
        String DICT_TYPE = "clocking_radius";
        SysDictData dictDatas = dictDataMapper.selectDictDataByType(DICT_TYPE).get(0);
        int clockingRadius = Integer.parseInt(dictDatas.getDictValue());
        // 按照行政与保安不同，分为两个数据表操作
        List<PtLineSiteApp> ptLineSiteApps = new ArrayList<>();
        if (SecurityUtils.getGuardId() != null) {
            lines = ptLineAppMapper.selectPtLineList(ptLine);
//            List<PtLineApp> history = ptLineAppMapper.selectPtLineHistoryList(ptLine);
//            lines.addAll(history);
            List<Long> objects = new ArrayList<>();
            for (PtLineApp line : lines) {
                objects.add(line.getScheduleId());
            }
            ptLineSiteApps = ptLineAppMapper.selectLineSiteList(objects);
        } else if (SecurityUtils.getStaffId() != null) {
            lines = ptLineStaffAppMapper.selectPtLineList(ptLine);
//            List<PtLineApp> history = ptLineStaffAppMapper.selectPtLineHistoryList(ptLine);
//            lines.addAll(history);
            List<Long> objects = new ArrayList<>();
            for (PtLineApp line : lines) {
                objects.add(line.getScheduleId());
            }
            ptLineSiteApps = ptLineStaffAppMapper.selectLineSiteList(objects);
        }


        // 添加半径
        if (lines != null) {
//            int index = 0;

//
            Long scheduleId = ptLineSiteApps.get(0).getScheduleId();
            List<PtLineSiteApp> objects = new ArrayList<>();
            Map<Long, List<PtLineSiteApp>> objectObjectHashMap = new HashMap<>();
            for (PtLineSiteApp ptLineSiteApp : ptLineSiteApps) {
                if (!ptLineSiteApp.getScheduleId().equals(scheduleId)) {
                    objectObjectHashMap.put(objects.get(0).getScheduleId(), objects);
                    objects = new ArrayList<>();
                    scheduleId = ptLineSiteApp.getScheduleId();
                }
                objects.add(ptLineSiteApp);
            }
            objectObjectHashMap.put(objects.get(0).getScheduleId(), objects);

            for (PtLineApp line : lines) {
                line.setLineSites(objectObjectHashMap.get(line.getScheduleId()));
            }
            lines.forEach(e -> e.setClockingRadius(clockingRadius));
        } else {
            lines = new ArrayList<>();
        }

        return lines;
    }

}
